package com.zjzaki.thingsboard.activity.main;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;

import com.github.mikephil.charting.charts.LineChart;
import com.google.android.material.tabs.TabLayout;
import com.zjzaki.thingsboard.R;
import com.zjzaki.thingsboard.ui.main.NoScrollViewPager;
import com.zjzaki.thingsboard.ui.main.SectionsPagerAdapter;
import com.zjzaki.thingsboard.util.FileHelper;
import com.zjzaki.thingsboard.util.IPUtil;
import com.zjzaki.thingsboard.databinding.DeviceHistorialDataBinding;

public class HistoricalDataActivity extends AppCompatActivity {

    /**
     * token值
     */
    private String token;
    /**
     * 设备ID
     */
    private String deviceId = "";
    /**
     * 设备名
     */
    private String deviceName = "";
    /**
     * 日志提示信息
     */
    private String TAG = "deviceInfo";
    /**
     * 服务器ip地址
     */
    private String ip = IPUtil.IP;
    //标题栏
    private Toolbar toolbar;
    //动作按钮
    ActionBar supportActionBar;
    //tab
    private DeviceHistorialDataBinding binding;

    //重写create方法
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //调用父类的方法
        super.onCreate(savedInstanceState);
        //设置页面
        // setContentView(R.layout.device_historial_data);
        binding = DeviceHistorialDataBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        //得到token值
        token = new FileHelper(this).getLoginMsg("login.txt").getToken();
        //初始化工具栏
        toolbar = findViewById(R.id.btn_return);
        //设置标题栏
        setSupportActionBar(toolbar);
        //得到当前的标题栏
        supportActionBar = getSupportActionBar();
        //设置返回键可见
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        //设置标题
        supportActionBar.setTitle("历史数据");

        //=====================================tab页相关=============================================
        //得到设备id
        String deviceId = getIntent().getStringExtra("deviceId");
        //得到设备名称
        String deviceName = getIntent().getStringExtra("deviceName");
        //创建选择页面适配器
        //SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this,
                getSupportFragmentManager(),deviceId,deviceName,token);
        //得到视图页
        NoScrollViewPager viewPager = binding.viewPager;
        //视图页设置适配器
        viewPager.setAdapter(sectionsPagerAdapter);
        //得到tab布局
        TabLayout tabs = binding.tabs;
        //tab设置视图页
        tabs.setupWithViewPager(viewPager);
        //阻止页面左右滑动
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });



    }

    /**
     * 返回键状态监听
     *
     * @param item 选择的item
     * @return 返回false以允许正常菜单处理继续，返回true以在此处使用它。
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
